package org.example.tmsapi.controller;

import org.example.tmsapi.model.ApiConfig;
import org.example.tmsapi.model.ResponseMessage;
import org.example.tmsapi.service.ApiConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/configs")
@CrossOrigin(origins = "*")
public class ApiConfigController {
    
    @Autowired
    private ApiConfigService apiConfigService;
    
    // 获取所有API配置
    @GetMapping
    public ResponseEntity<?> getAllApiConfigs() {
        List<ApiConfig> configs = apiConfigService.getAllApiConfigs();
        return ResponseEntity.ok(ResponseMessage.success(configs));
    }
    
    // 根据key获取API配置
    @GetMapping("/{key}")
    public ResponseEntity<?> getApiConfigByKey(@PathVariable String key) {
        ApiConfig config = apiConfigService.getApiConfigByKey(key);
        if (config == null) {
            return ResponseEntity.status(404).body(ResponseMessage.error("配置项不存在"));
        }
        return ResponseEntity.ok(ResponseMessage.success(config));
    }
    
    // 更新API配置
    @PutMapping
    public ResponseEntity<?> updateApiConfig(@RequestBody ApiConfig config) {
        boolean success = apiConfigService.updateApiConfig(config);
        if (!success) {
            return ResponseEntity.status(400).body(ResponseMessage.error("更新失败，请检查配置参数"));
        }
        return ResponseEntity.ok(ResponseMessage.success("配置更新成功"));
    }
}